﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;

public class UIShop : MonoBehaviour 
{
    private Button btn_CloseSelf;
    private Text text_DiamondCount;// 玩家总共的钻石数量
    private Text text_SkinName;
    private Transform grid;
    private int indexSkin;
    private Button btn_Select;
    private Button btn_Buy;
    private Text text_SpendDiamond;


    private void Awake()
    {
        EventCenter.AddListener(CL.EventType.ShowUIShop, ShowUIShop);
        EventCenter.AddListener(CL.EventType.HideUIShop, HideUIShop);
        EventCenter.AddListener(CL.EventType.UpdateDiamondCountUI, UpdateDiamondCountUI);
        EventCenter.AddListener<int>(CL.EventType.UpdateSkinUnlockStateUI, UpdateSkinUnlockStateUI);


        btn_CloseSelf = transform.Find("Top/Btn_CloseSelf").GetComponent<Button>();
        btn_CloseSelf.onClick.AddListener(Btn_CloseSelf);
        text_DiamondCount = transform.Find("Top/Diamond/Text").GetComponent<Text>();
        text_SkinName = transform.Find("Center/Text_SkinName").GetComponent<Text>();
        grid = transform.Find("Center/Scroll View/Grid");
        btn_Select = transform.Find("Bottom/Btn_Select").GetComponent<Button>();
        btn_Select.onClick.AddListener(Btn_Select);
        btn_Buy = transform.Find("Bottom/Btn_Buy").GetComponent<Button>();
        btn_Buy.onClick.AddListener(Btn_Buy);
        text_SpendDiamond = transform.Find("Bottom/Btn_Buy/Diamond/Text").GetComponent<Text>();


        Init();
    }

    /// <summary>
    /// 初始化UIShop，克隆生成皮肤，
    /// </summary>
    private void Init()
    {
        //Grid长与宽根据皮肤的数量自适应，两边留出160宽度值，皮肤数量可随时更改
        grid.GetComponent<RectTransform>().sizeDelta = new Vector2((ResManager.Instance.playerSkin.Length + 2) * 160, 300);
        SpawnManager.Instance.SpawnPlayerSkin(grid);
    }

    private void OnDestroy()
    {
        EventCenter.RemoveListener(CL.EventType.ShowUIShop, ShowUIShop);
        EventCenter.RemoveListener(CL.EventType.HideUIShop, HideUIShop);
        EventCenter.RemoveListener(CL.EventType.UpdateDiamondCountUI, UpdateDiamondCountUI);
        EventCenter.RemoveListener<int>(CL.EventType.UpdateSkinUnlockStateUI, UpdateSkinUnlockStateUI);
    }

    private void Update()
    {
        indexSkin = (int)Mathf.Round(grid.localPosition.x / -160f);
        if (Input.GetMouseButtonUp(0))
        {
            
            //grid.localPosition = new Vector3(indexSkin * -160, 0, 0);
            grid.DOLocalMoveX(indexSkin * -160, 0.3f);            
        }
        ScrollViewPlayerSkin(indexSkin);
    }


    /// <summary>
    /// 显示、隐藏UIShop
    /// </summary>
    private void ShowUIShop()
    {
        gameObject.SetActive(true);
    }
    private void HideUIShop()
    {
        gameObject.SetActive(false);
    }

    /// <summary>
    /// 关闭商店按钮点击
    /// </summary>
    private void Btn_CloseSelf()
    {
        AudioManager.Instance.PlaySound("button");
        GameManager.Instance.UIShopClose();
    }
    /// <summary>
    /// 滑动选择玩家皮肤
    /// </summary>
    private void ScrollViewPlayerSkin(int index)
    {
        UpdateSkinUnlockStateUI(index);
        for (int i = 0; i < grid.childCount; i++)
        {            
            if (index == i)
            {                
                UpdateSkinNameUI(i);               
                grid.GetChild(i).GetChild(0).GetComponent<RectTransform>().sizeDelta = new Vector2(160, 160);
            }
            else
            {
                grid.GetChild(i).GetChild(0).GetComponent<RectTransform>().sizeDelta = new Vector2(80, 80);
            }
        }
    }
    /// <summary>
    /// 选择皮肤按钮开始游戏回到主界面
    /// </summary>
    private void Btn_Select()
    {
        AudioManager.Instance.PlaySound("button");
        GameManager.Instance.UIShopClose();
        DataManager.Instance.SelectPlayerSkin(indexSkin);
    }
    /// <summary>
    /// 购买皮肤按钮
    /// </summary>
    private void Btn_Buy()
    {
        AudioManager.Instance.PlaySound("button");
        if (DataManager.Instance.mUserData.diamondCount < DataManager.Instance.mUserData.skinUnlockDiamond[indexSkin])
        {
            print("钻石不够");
            return;
        }
        DataManager.Instance.BuyPlayerSkin(indexSkin);
    }

    /// <summary>
    /// 更新拥有钻石数量UI
    /// </summary>
    private void UpdateDiamondCountUI()
    {
        text_DiamondCount.text = DataManager.Instance.mUserData.diamondCount.ToString();
    }

    /// <summary>
    /// 更新皮肤名字UI显示
    /// </summary>
    /// <param name="i"></param>
    private void UpdateSkinNameUI(int i)
    {
        text_SkinName.text = ResManager.Instance.skinName[i];
    }

    /// <summary>
    /// 更新皮肤解锁状态及所需解锁钻石消耗Text
    /// </summary>
    private void UpdateSkinUnlockStateUI(int index)
    {
        if (DataManager.Instance.mUserData.skinUnlockState[index])
        {
            grid.GetChild(index).GetChild(0).GetComponent<Image>().color = Color.white;
            btn_Buy.gameObject.SetActive(false);
            btn_Select.gameObject.SetActive(true);              
        }
        else
        {
            grid.GetChild(index).GetChild(0).GetComponent<Image>().color = Color.gray;
            btn_Buy.gameObject.SetActive(true);
            text_SpendDiamond.text = DataManager.Instance.mUserData.skinUnlockDiamond[index].ToString();
            btn_Select.gameObject.SetActive(false);            
        }
    }

   
}
